home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / macurl2path.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  3.0 KB  |  90 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Mac specific module for conversion between pathnames and URLs.
  5. Do not import directly, use urllib instead.'''
  6. import string
  7. import urllib
  8. import os
  9.  
  10. def url2pathname(pathname):
  11.     '''Convert /-delimited pathname to mac pathname'''
  12.     tp = urllib.splittype(pathname)[0]
  13.     if tp and tp != 'file':
  14.         raise RuntimeError, 'Cannot convert non-local URL to pathname'
  15.     
  16.     components = string.split(pathname, '/')
  17.     i = 0
  18.     while i < len(components):
  19.         if components[i] == '.':
  20.             del components[i]
  21.         elif components[i] == '..' and i > 0 and components[i - 1] not in ('', '..'):
  22.             del components[i - 1:i + 1]
  23.             i = i - 1
  24.         elif components[i] == '' and i > 0 and components[i - 1] != '':
  25.             del components[i]
  26.         else:
  27.             i = i + 1
  28.     if not components[0]:
  29.         rv = string.join(components[1:], ':')
  30.     else:
  31.         i = 0
  32.         while i < len(components) and components[i] == '..':
  33.             components[i] = ''
  34.             i = i + 1
  35.         rv = ':' + string.join(components, ':')
  36.     return urllib.unquote(rv)
  37.  
  38.  
  39. def pathname2url(pathname):
  40.     '''convert mac pathname to /-delimited pathname'''
  41.     if '/' in pathname:
  42.         raise RuntimeError, 'Cannot convert pathname containing slashes'
  43.     
  44.     components = string.split(pathname, ':')
  45.     if components[0] == '':
  46.         del components[0]
  47.     
  48.     if components[-1] == '':
  49.         del components[-1]
  50.     
  51.     for i in range(len(components)):
  52.         pass
  53.     
  54.     components = map(_pncomp2url, components)
  55.     if os.path.isabs(pathname):
  56.         return '/' + string.join(components, '/')
  57.     else:
  58.         return string.join(components, '/')
  59.  
  60.  
  61. def _pncomp2url(component):
  62.     component = urllib.quote(component[:31], safe = '')
  63.     return component
  64.  
  65.  
  66. def test():
  67.     for url in [
  68.         'index.html',
  69.         'bar/index.html',
  70.         '/foo/bar/index.html',
  71.         '/foo/bar/',
  72.         '/']:
  73.         print `url`, '->', `url2pathname(url)`
  74.     
  75.     for path in [
  76.         'drive:',
  77.         'drive:dir:',
  78.         'drive:dir:file',
  79.         'drive:file',
  80.         'file',
  81.         ':file',
  82.         ':dir:',
  83.         ':dir:file']:
  84.         print `path`, '->', `pathname2url(path)`
  85.     
  86.  
  87. if __name__ == '__main__':
  88.     test()
  89.  
  90.